import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

/**
 * Class to manage the distanceMatrix
 * @author marcotezzele
 *
 */
public class Map {

	private double[][] distanceMatrix;
	
	/**
	 * Create the matrix and initialize it to 0.
	 * @param nodesNumber Number of nodes
	 */
	public Map(int nodesNumber) {
		this.distanceMatrix = new double[nodesNumber][nodesNumber];
		for (int i = 0; i < nodesNumber; i++)
			for (int j = 0; j < nodesNumber; j++)
				this.distanceMatrix[i][j] = 0;
	}
	
	/**
	 * Set the distanceMatrix[i][j] value equal to distance 
	 * @param i row index
	 * @param j column index
	 * @param distance distance from i to j
	 */
	public void setDistance(int i, int j, double distance)		
		throws IllegalArgumentException {
			
		if (i == j && distance != 0) {
			throw new IllegalArgumentException("The distance from " + i + 
					" to " + j + " must be equal to 0. Control the " +
					"distanceMatrix assembling.");
			}
		this.distanceMatrix[i][j] = distance;
	}
	
	/**
	 * Read the double values of distanceMatrix from a text file.
	 * @param filename Name of the input file
	 * @param nodesNumber Number of nodes
	 * @throws FileNotFoundException
	 * TODO controllo sul tipo di dato letto
	 * TODO controllo sull'effettiva lettura di tutti i valori
	 */
	public void readFile(String filename, int nodesNumber) throws FileNotFoundException {
		File inFile = new File(filename);
		Scanner in = new Scanner(inFile);
		
		for (int i = 0; i < nodesNumber; i++) {
			for (int j = 0; j < nodesNumber; j++) {
				this.setDistance(i, j, in.nextDouble());
			}
		}
	}
	
	/**
	 * Return the distance from i to j
	 * @param i row index
	 * @param j column index
	 * @return distance from i to j
	 */
	public double getDistance(int i, int j) {
		return this.distanceMatrix[i][j];
	}
	
	/**
	 * Put the distanceMatrix in a String.
	 * Use it with System.out.println();
	 * @param nodesNumber number of nodes
	 * @return String that contains the distanceMatrix
	 */
	public String toString(int nodesNumber) {
		String r = "";
		for (int i = 0; i < nodesNumber; i++) {
			for (int j = 0; j < nodesNumber; j++) {
				r = r + this.distanceMatrix[i][j] + " ";
			}
			r = r + "\n";
		}
		return r;
	}
	
}
